$(function (){
    getLb(1, '.jq_lei');
    getLb(2, '.jq_er_lei');
    getLb(3, '.jq_duan_lei');
    getLb1(1, '.jq_lei_choce');
})
//封装获取的类别
function getLb(type, obj) {
    $.ajax({
        url: baseNews +"/api/data/sys/type/"+type,
        success:function (res){
            console.log(res)
            if(res.code==0){
                $(obj).empty();
                var html="";
                $(res.data).each(function (index,key){
                    console.log(key)
                    html +=`<tr>
                        <td>${key.id}</td>
                        <td>${key.tname}</td>
                        <td>${key.num}</td>
                        <td><span class="lb_xg" oname="${key.tname}" oid="${key.id}">修改<span/></td>
                        <td><span class="lb_sc" oid="${key.id}">删除<span/></td>
                    </tr>`;
                })
                $(obj).append(html);
            }
        }
    })
}

$(function (){
    //删除
    $(document).on('click',".lb_sc",function (){
        var ids = $(this).attr("oid");
        zeroModal.confirm("确定要删除么？", function () {
            var json = {}
            var result = JSON.stringify(json);
            $.ajax({
                url: baseNews + "/api/data/sys/del/type/?ids=" + ids,
                type: "post",
                headers: { 'Content-Type': 'application/json' },
                dataType: "json",
                data: result,
                success: function (res) {
                    if (res.code == 0) {
                        location.reload();
                    } else {
                        zeroModal.alert(res.message);
                    }
                }
            })
        })
    })
    //ej_add
})
$(function (){
    //添加基础类别
    $(".add_jc").click(function (){
        var json = {};
        var jc_name = $(".jc_name").val();
        var result = JSON.stringify(json);
        if (jc_name == "") {
            zeroModal.alert("请输入二级类别");
            return false;
        }
        $.ajax({
            url: baseNews + "/api/data/sys/add/type/1?tname=" + jc_name,
            type: "post",
            headers: { 'Content-Type': 'application/json' },
            dataType: "json",
            data: result,
            success: function (res) {
                if (res.code == 0) {
                    location.reload();
                } else {
                    zeroModal.alert(res.message);
                }
            }
        })
    })
    //添加二级类别
    $(".ej_add").click(function () {
        var json = {};
        var ej_leibe = $(".ej_leibe").val();
        var result = JSON.stringify(json);
        var numCount=0;
        var ids="";
        $(".arc_isc").each(function (index, key) {
            if ($(this).prop("checked") == true) {
                numCount++;
                ids += $(this).attr("oid") + ',';
            }
        })
        ids = ids.substring(0, ids.length - 1);

        console.log(ids,2222222)
        if (numCount<1){
            zeroModal.alert("请选择基础类别");
            return false;
        }
        if (ej_leibe==""){
            zeroModal.alert("请输入二级类别");
            return false;
        }
        $.ajax({
            url: baseNews + "/api/data/sys/add/type/2?tname=" + ej_leibe + "&parent=" + ids,
            type: "post",
            headers: { 'Content-Type': 'application/json' },
            dataType: "json",
            data: result,
            success: function (res) {
                if (res.code == 0) {
                    location.reload();
                } else {
                    zeroModal.alert(res.message);
                }
            }
        })
    })
    //qx
    $('.qxl').click(function (){
        location.reload();
    })
    $(".add_dp_lei").on('click',function (){
        var json = {};
        var dp_leibie = $(".dp_leibie").val();
        var result = JSON.stringify(json);
        if (dp_leibie == "") {
            zeroModal.alert("请输入短评类别");
            return false;
        }
        $.ajax({
            url: baseNews + "/api/data/sys/add/type/3?tname=" + dp_leibie,
            type: "post",
            headers: { 'Content-Type': 'application/json' },
            dataType: "json",
            data: result,
            success: function (res) {
                if (res.code == 0) {
                    location.reload();
                } else {
                    zeroModal.alert(res.message);
                }
            }
        })
    })
})
//封装获取的类别
function getLb1(type, obj) {
    $.ajax({
        url: baseNews + "/api/data/sys/type/" + type,
        success: function (res) {
            console.log(res)
            if (res.code == 0) {
                $(obj).empty();
                var html = "";
                $(res.data).each(function (index, key) {
                    console.log(key)
                    html += `<tr>
                        <td><input type="checkbox" oid="${key.id}" class="arc_isc" /></td>
                        <td>${key.id}</td>
                        <td>${key.tname}</td>
                        <td>${key.num}</td>
                    </tr>`;
                })
                $(obj).append(html);
            }
        }
    })
}
$(function (){
    // 修改 基础
    $(document).on('click','.jq_lei .lb_xg',function (){
        var oId=$(this).attr("oid");
        $(".right_one>div").hide();
        $(".qx_one").show();
        $(".jc_name").val($(this).attr("oname"));
        $(".qx_oid").val(oId);
        //获取二级类别默认选中
        
    })
    //修改提交
    $(".xg_lienoe").on('click',function (){
        var oid = $(".qx_oid").val();
        var json = {};
        var jc_name = $(".jc_name1").val();
        var result = JSON.stringify(json);
        if (jc_name == "") {
            zeroModal.alert("请输入二级类别");
            return false;
        }
        $.ajax({
            url: baseNews + "/api/data/sys/update/type/1?id=" + oid + "&tname=" + jc_name,
            type: "post",
            headers: { 'Content-Type': 'application/json' },
            dataType: "json",
            data: result,
            success: function (res) {
                if (res.code == 0) {
                    location.reload();
                    console.log(res,8888)
                } else {
                    zeroModal.alert(res.message);
                }
            }
        })
    })
    // 修改 短评
    $(document).on('click', '.jq_duan_lei .lb_xg', function () {
        var oId = $(this).attr("oid");
        $(".right_one>div").hide();
        $(".dp_lei").show();
        $(".dp_leibie1").val($(this).attr("oname"));
        $(".dp_oid").val(oId);
    })
    //修改提交
    $(".add_dp_leiqx").on('click', function () {
        var oid = $(".dp_oid").val();
        var json = {};
        var jc_name = $(".dp_leibie1").val();
        var result = JSON.stringify(json);
        if (jc_name == "") {
            zeroModal.alert("请输入短评类别");
            return false;
        }
        $.ajax({
            url: baseNews + "/api/data/sys/update/type/3?id=" + oid + "&tname=" + jc_name,
            type: "post",
            headers: { 'Content-Type': 'application/json' },
            dataType: "json",
            data: result,
            success: function (res) {
                if (res.code == 0) {
                    location.reload();
                    console.log(res, 8888)
                } else {
                    zeroModal.alert(res.message);
                }
            }
        })
    })
    // 修改 二级
    $(document).on('click', '.jq_er_lei .lb_xg', function () {
        var oId = $(this).attr("oid");
        $(".right_one>div").hide();
        $(".change_ej").show();
        $(".ej_leibe1").val($(this).attr("oname"));
        $(".ej_oid").val(oId);
        $.ajax({
            url:baseNews+"/api/data/sys/type/info/"+oId,
            success:function(res){
                console.log(res,321);
               var arr=  res.data.parent.match(/\d+/g);
                for(var i=0;i<arr.length;i++){
                    $(".jq_lei_choce tr").each(function (index,key){
                      console.log($(key).find("td").eq(1).html(),888)
                      var s=$(key).find("td").eq(1).html();
                      if(arr[i]==s){
                        $(key).find("td").eq(0).find("input").prop('checked',"checked")
                      }
                    })
                }
            }
        })
    })
    //修改提交
    $(".xj_ej_s").on('click', function () {
        var oid = $(".ej_oid").val();
        var json = {};
        var jc_name = $(".ej_leibe1").val();
        var result = JSON.stringify(json);
        if (jc_name == "") {
            zeroModal.alert("请输入二级类别");
            return false;
        }
        var ids = "";
        var numCount=0;
        $(".change_ej .arc_isc").each(function (index, key) {
            if ($(this).prop("checked") == true) {
                numCount++;
                ids = ids + $(this).attr("oid") + ',';
            }
        })
        ids = ids.substring(0, ids.length - 1);
        $.ajax({
            url: baseNews + "/api/data/sys/update/type/2?id=" + oid + "&tname=" + jc_name + "&parent=" + ids,
            type: "post",
            headers: { 'Content-Type': 'application/json' },
            dataType: "json",
            data: result,
            success: function (res) {
                if (res.code == 0) {
                    location.reload();
                    console.log(res, 8888)
                } else {
                    zeroModal.alert(res.message);
                }
            }
        })
    })
    
})